﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Windows.Forms;

namespace MyWindowsMediaPlayer.Libs.Conf
{
	public sealed class CustomSection : ConfigurationSection
	{
		public CustomSection ( )
		{
		}

		[ConfigurationProperty("Name",
		    DefaultValue = "MyWindowsMediaPlayer",
		    IsRequired = true,
		    IsKey = true)]
		public string Name
		{
			get { return (string)this[ "name" ]; }
		}

		[ConfigurationProperty("moviePath",
		    DefaultValue = "",
		    IsRequired = true,
		    IsKey = true)]
		public string MoviePath
		{
			get { return (string)this[ "moviePath" ]; }
			set { this[ "moviePath" ] += value + ";"; }
		}

		[ConfigurationProperty("picturePath",
		   DefaultValue = "",
		   IsRequired = true,
		   IsKey = true)]
		public string PicturePath
		{
			get { return (string)this[ "picturePath" ]; }
			set { this[ "picturePath" ] += value + ";"; }
		}

		[ConfigurationProperty("musicPath",
		 DefaultValue = "",
		 IsRequired = true,
		 IsKey = true)]
		public string MusicPath
		{
			get { return (string)this[ "musicPath" ]; }
			set { this[ "musicPath" ] += value + ";"; }
		}
	}

	class ManageConf
	{
		private Configuration _config;
		
		public ManageConf ( )
		{
			#if DEBUG
			string applicationName =
			    Environment.GetCommandLineArgs()[ 0 ];
			#else
				    string applicationName =
				   Environment.GetCommandLineArgs()[0]+ ".exe";
			#endif

			string exePath = System.IO.Path.Combine(
			    Environment.CurrentDirectory, applicationName);

			CustomSection section = new CustomSection();

			this._config = ConfigurationManager.OpenExeConfiguration(exePath) as Configuration;

			section = this._config.GetSection("CustomSection") as CustomSection;
			this.initConf();
			
		}

		public void initConf ( )
		{
			if (this._config.Sections[ "CustomSection" ] == null)
			{
				CustomSection section = new CustomSection();

				this._config.Sections.Add("CustomSection", section);
				section.SectionInformation.ForceSave = true;
				this._config.Save(ConfigurationSaveMode.Full);
				ConfigurationManager.RefreshSection("CustomSection");
			}
		}

		public string MusicPath
		{
			get
			{
				CustomSection section = _config.Sections[ "CustomSection" ] as CustomSection;
				return (section != null ? section.MusicPath : null);
			}
			set
			{
				CustomSection section = this._config.Sections[ "CustomSection" ] as CustomSection;

				if (section == null)
				{
					section = new CustomSection();
					_config.Sections.Add("CustomSection", section);
				}
				section.MusicPath = value;
				section.SectionInformation.ForceSave = true;
				_config.Save(ConfigurationSaveMode.Full);
				ConfigurationManager.RefreshSection("CustomSection");
				MessageBox.Show(section.MusicPath);
				MessageBox.Show(value);
			}
		}

		public string PicturePath
		{
			get
			{
				CustomSection section = _config.Sections[ "CustomSection" ] as CustomSection;
				return (section != null ? section.PicturePath : null);
			}
			set
			{
				CustomSection section = this._config.Sections[ "CustomSection" ] as CustomSection;

				if (section == null)
					_config.Sections.Add("CustomSection", section);
				section.PicturePath = value;
				section.SectionInformation.ForceSave = true;
				_config.Save(ConfigurationSaveMode.Full);
			}
		}

		public string MoviePath
		{
			get
			{
				CustomSection section = _config.Sections[ "CustomSection" ] as CustomSection;
				return (section != null ? section.MoviePath : null);
			}
			set
			{
				CustomSection section = this._config.Sections[ "CustomSection" ] as CustomSection;

				//if (section == null)
				//    _config.Sections.Add("CustomSection", section);
				//section.MoviePath = value;

				//section.SectionInformation.ForceSave = true;
				//_config.Save(ConfigurationSaveMode.Modified);
				section.MoviePath = value;
				_config.AppSettings.Settings.Add("MoviePath", section.MoviePath);
				_config.Save(ConfigurationSaveMode.Full);
			}
		}
	}
}
